var _param = parse_url(window.location.href);
var _config = {
	OrderStatus : _param.type ? _param.type :"0" , //0 全部 1 待支付  2 配送中 3 已完成 4 已取消
	PageIndex:1, 
	isAjax:true,
	TotalCount:"",
	PageSize : 10
};
var _option = {
	windowH :$(window).height(),
	scrollH :null,
	documentH : null
};
var $navList =$("#navList");
var $orderList = $("#orderList");
var $ajaxSatus = $("<div>",{"class":"ajaxSatus"}).text("正在加载");
$orderList.after($ajaxSatus);
$("a",$navList).eq(_config.OrderStatus).addClass("on").siblings().removeClass("on");
define(function(){
	return {
		"init" : function(){
			var _this = this;
	        this.ajaxload();
	        $(window).on("scroll touchmove",function(){
	        	if(_config.isAjax){
		        	if($(window).scrollTop()+100 > ( _option.documentH - _option.windowH )){
		        		_config.isAjax = false;
		        		_this.ajaxload();
		        	}
	        	};
	        });
		},
		"ajaxload":function(){
			//订单列表
	        $.ajax({
	            url: _CONFIG[__webState].AJAX_URL + "api/BookOrder/OrderList",
	            type: "post",
	            dataType: "json",
	            data: _config
	        }).done(function(data){
	        	console.log(data);
	        	if(data.Code == 0){
	        		if(data.Data && data.Data.OrderList != ""){
	        			++_config.PageIndex;
	        			_config.TotalCount = data.Data.TotalCount;
	        			$orderList.append(orderHtml(data.Data.OrderList));
	        			_option.documentH = $(document).height();
		        		_config.isAjax = true;
	        		}else{
	        			_config.isAjax = false;
	        			if(_config.PageIndex == 1){
	        				$orderList.html(_CONFIG[__webState].noOrderData);
	        				$ajaxSatus.remove();
	        			}else{
	        				$ajaxSatus.html("没有更多了");
	        			}
	        		}
	        	}else{
	        		popWrap.alert({content:data.Message})
	        	}
	        }).fail(
	        function (xhr, textStatus, err) {
	            popWrap.alert({content:err})
	        }).always(function(){
				ajaxLoading.remove();
	        });

			//订单状态
			__base.AjaxFun({
				url: __base.url.GetPersonOrderStatus
			},function(data){
				console.log(data);
				if (data["Code"] === "0" ){
					var $elem =  $(".navList").find(">a"),
							html = function( count ){
								return '<span class="count_dd">' + count + '</span>';
							};
					$elem.eq(1).append( html(data['Data']['WaitTotal']) );
					$elem.eq(2).append( html(data['Data']['AlreadyShipped']) );
				}
			});


		}
	}
});
//orderTypeUnicode = ["&#xe901;","&#xe903;","&#xe902;","&#xe904;"];//待支付，已完成，配送中，已取消
function orderHtml(data){
	var obj = [];
	$.each(data, function(v,k) {
		var CreateDateTime = k.CreateDateTime.replace("T"," ").substr(0,19);
		var orderTypeIcon = "";
		var btn = "";
		switch (k.OrderStatus){
			case 0:
				orderTypeIcon = "&#xe901;";
				break;
			case 20:
				orderTypeIcon = "&#xe903;";
				btn = '<a href="' + _CONFIG[__webState].orderAfterSales + '?orderNo=' + k['OrderNo'] + '" class="btn">申请退货</a>';
				break;
			case 19:
				orderTypeIcon = "&#xe902;";
				break;
			case 14:
				orderTypeIcon = "&#xe904;";
				break;	
			default:
				break;
		};
		obj.push('<div class="list">',
			'<div class="state">',
				'<div class="time">'+CreateDateTime+'</div>',
				btn,
				'<div class="stateTime"><span class="c1">'+orderTypeIcon+'</span><em>'+k.OrderStatusStr+'</em></div>',
			'</div>',
				'<a href="'+ _CONFIG[__webState].orderDetails+"?orderNo="+k.OrderNo+'">',
				(function(fng){
					var html = "";
					$.each(fng,function(_v,_k){
						html+='<div class="title">';
						html += '<span><img src="'+_k.Pic+'"></span>';
						html += '<p>'+_k.ProductName+'</p>';
						html+='</div>';
					})
					return html;
				})(k.ProductList),
				'</a>',

				'<div class="detailed">',
				'<em>需支付：</em>',
				'<span>积分<b>'+k.Score+'</b> +现金<b>'+k.Amount+'</b></span>',
			'</div>',
		'</div>');
	});
	return obj.join('');
}
